package com.weiadmin.response;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回数据格式
 * @author: shizhongyu
 * @date: 2023年03月18日 13:27
 */
public class RestResponse<T> {

    public ResponseEntity<?> success(String message){

        Map<String, Object> resultBody = new HashMap<>();

        resultBody.put("code",1);
        resultBody.put("msg",message);
        return ResponseEntity.status(HttpStatus.OK)
                .body(resultBody);
    }
    public ResponseEntity<?> page(String message,T data,long count){
        Map<String, Object> resultBody = new HashMap<>();
        resultBody.put("code",1);
        resultBody.put("count",count);
        resultBody.put("msg",message);
        resultBody.put("data",data);
        return ResponseEntity.status(HttpStatus.OK)
                .body(resultBody);
    }
    public ResponseEntity<?> success(String message,T data){

        Map<String, Object> resultBody = new HashMap<>();
        resultBody.put("error",0);
        resultBody.put("code",1);
        resultBody.put("count",1);
        resultBody.put("msg",message);
        resultBody.put("data",data);
        return ResponseEntity.status(HttpStatus.OK)
                .body(resultBody);
    }
    public ResponseEntity<?> error(String message,T data){
        Map<String, Object> resultBody = new HashMap<>();
        resultBody.put("error",1);
        resultBody.put("code","0");
        resultBody.put("msg",message);
        resultBody.put("data",data);

        return ResponseEntity.status(HttpStatus.OK)
                .body(resultBody);
    }

    public ResponseEntity<?> error(String message){
        Map<String, Object> resultBody = new HashMap<>();
        resultBody.put("code",0);
        resultBody.put("msg",message);
        return ResponseEntity.status(HttpStatus.OK)
                .body(resultBody);
    }
}
